package com.leo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @ClassName ConnectionGrabBarrage
 * @Description
 * @Author wangss
 * @date 2020.11.15 19:49
 * @Version 1.0
 */
public class ConnectionGrabBarrage {

    private String request_url;
    private String request_number;
    private HttpURLConnection conn;

    public ConnectionGrabBarrage() {

    }

    public ConnectionGrabBarrage(String request_url, String request_number) {
        this.request_url = request_url;
        this.request_number = request_number;
    }

    public boolean connection() {
        try {
            URL url = new URL(request_url);
            conn = (HttpURLConnection) url.openConnection();

            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("post");

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.connect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public String getData() {
        try (OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream())) {
            String post_value1 = "roomid=" + URLEncoder.encode("7735393", "UTF-8");
            String post_value2 = "&csrf_token=" + URLEncoder.encode("7735393", "UTF-8");
            String post_value3 = "&csrf=" + URLEncoder.encode("7735393", "UTF-8");
            String post_value4 = "&visit_id=" + URLEncoder.encode("", "UTF-8");

            String post_value = post_value1 + post_value2 + post_value3 + post_value4;

            writer.write(post_value);
            writer.flush();

            String result = "";
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((result = br.readLine()) != null) {

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    public String getRequest_url() {
        return request_url;
    }

    public void setRequest_url(String request_url) {
        this.request_url = request_url;
    }

    public String getRequest_number() {
        return request_number;
    }

    public void setRequest_number(String request_number) {
        this.request_number = request_number;
    }

    public HttpURLConnection getConn() {
        return conn;
    }

    public void setConn(HttpURLConnection conn) {
        this.conn = conn;
    }
}
